﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Web.Core
{
    public class Common
    {
        /// <summary>
        /// md5加密方式
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="code">16，32 位</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5(string str, int code)
        {
            if (code == 16)
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToLower().Substring(8, 16);
            }
            if (code == 32)
            {
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();
            }
            return str;
        }

        /// <summary>
        /// 生成随机号
        /// </summary>
        public static string RandomStr(int i)
        {
            string temp = DateTime.Now.ToString();
            temp = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(temp, "MD5");
            return temp.Substring(0, i).ToUpper();
        }

         /// <summary>
        /// 得到客户端IP
        /// </summary>
        /// <returns>加密后的字符串</returns>
        public static string GetIP()
        {
            return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() ;
        }

        /// <summary>
        ///SQL注入过滤
        /// </summary>
        /// <param name="InText">要过滤的字符串</param>
        /// <returns>如果参数存在不安全字符，则返回true</returns>
        public static bool SqlFilter(string InText)
        {
            string word = "and|exec|insert|select|delete|update|chr|mid|master|or|truncate|char|declare|join";
            if (InText == null)
                return false;
            foreach (string i in word.Split('|'))
            {
                if ((InText.ToLower().IndexOf(i + " ") > -1) || (InText.ToLower().IndexOf(" " + i) > -1))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 转换HTML到文本
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string HTML2Text(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = Content.Replace("<", "&lt;");
                Content = Content.Replace(">", "&gt;");
            }
            return Content;
        }

        /// <summary>
        /// 过滤HTML
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearHTML(string Content)
        {
            if ((Content != null) && (Content != string.Empty))
            {
                Content = Regex.Replace(Content, "<[^>]+>", "");
            }
            return Content;
        }

        /// <summary>
        /// 生成调用ajax的代码
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public static string RegAjax(Type type)
        {
            string assemblyName = type.FullName + "," + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
            if (type.Assembly.FullName.StartsWith("App_Code."))
                assemblyName = type.FullName + ",App_Code";
            string str = "<script type=\"text/javascript\" src=\"/ajaxpro/prototype.ashx\"></script>\r\n";
            str += "<script type=\"text/javascript\" src=\"/ajaxpro/core.ashx\"></script>\r\n";
            str += "<script type=\"text/javascript\" src=\"/ajaxpro/converter.ashx\"></script>\r\n";
            str += "<script type=\"text/javascript\" src=\"/ajaxpro/" + assemblyName + ".ashx\"></script>\r\n";
            return str;
        }
    }
}